package cn.config;

import cn.interceptor.LoginInterceptor;
import cn.interceptor.ResourceInterceptor;
import cn.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("cn.controller")
@EnableWebMvc

public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    SysResourceService sysResourceService;
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        return new InternalResourceViewResolver("/WEB-INF/html/",".html");
    }

    @Bean("multipartResolver")
    public CommonsMultipartResolver getMultipartResolver(){
        return new CommonsMultipartResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        InterceptorRegistration registration = registry.addInterceptor(loginInterceptor);
        registration.addPathPatterns("/**");
        registration.excludePathPatterns(new String[]{"/common/**"});
        ResourceInterceptor resourceInterceptor = new ResourceInterceptor(sysResourceService);
        InterceptorRegistration registration2 = registry.addInterceptor(resourceInterceptor);
        registration2.addPathPatterns("/**");
    }
}
